procedure parser (var ktot: integer; var ty: hdlstringarray0; var typ: hdlstringarray0; var typr: hdlintarray0; var nodetable: hdlnoderecord; var numnodes: integer; var error: str255);
implementation
procedure parser;
label
992, 993;
var
i, j, k, l, m, n, del, jtot: integer;
s1, s2, s3: boolean;
procedure setnodefields (l, m, n: integer);
begin
numnodes := numnodes + 1;
nodetable^^[numnodes].optype := typ^^[l];
nodetable^^[numnodes].loptype := typ^^[m];
nodetable^^[numnodes].roptype := typ^^[n];
nodetable^^[numnodes].op.index := ty^^[l];
nodetable^^[numnodes].lop.index := ty^^[m];
nodetable^^[numnodes].rop.index := ty^^[n];
end;
procedure reset (l, m, n: integer);
var
k: integer;
begin
jtot := jtot - n;
for k := l to m do
begin
ty^^[k] := ty^^[k + n];
typr^^[k] := typr^^[k + n];
typ^^[k] := typ^^[k + n];
end;
end;
procedure setnodetoken (l: integer);
begin
ty^^[l] := stringof(numnodes : 2);
typ^^[l] := 'node';
typr^^[l] := 0;
end;
begin
error := '';
jtot := ktot;
numnodes := 0;
j := 0;
repeat
j := j + 1;
if j < 1 then
j := 1;
s1 := (typ^^[j + 1] = 'constant') or (typ^^[j + 1] = 'variable') or (typ^^[j + 1] = 'real') or (typ^^[j + 1] = 'node');
s2 := (typ^^[j - 1] = 'constant') or (typ^^[j - 1] = 'variable') or (typ^^[j - 1] = 'real') or (typ^^[j - 1] = 'node');
s3 := (typ^^[j - 3] = 'constant') or (typ^^[j - 3] = 'variable') or (typ^^[j - 3] = 'real') or (typ^^[j - 3] = 'node');
if ((typ^^[j] = 'unary') or (typ^^[j] = 'function')) and s1 then
begin
setnodefields(j, j + 1, j + 1);
setnodetoken(j);
reset(j + 1, jtot, 1);
j := j - 2;
goto 992;
end;
if (ty^^[j] = quote) and s2 then
begin
setnodefields(j, j - 1, j - 1);
setnodetoken(j - 1);
j := j - 1;
reset(j + 1, jtot, 1);
j := j - 2;
goto 992;
end;
if (typ^^[j] = 'binary') and (ty^^[j] <> '(') then
begin
if (j - 2 >= 0) and (typ^^[j - 2] <> 'binary') and (typ^^[j - 2] <> 'unary') and (typ^^[j - 2] <> 'function') then
begin
error := concat(ty^^[j - 2], ' is not a binary token ');
goto 993;
end;
while (j - 2 >= 0) and (typr^^[j - 2] >= typr^^[j]) and (typ^^[j - 2] <> 'unary') and (typ^^[j - 2] <> 'function') do
begin
if (not s2) and (not s3) then
begin
error := concat(ty^^[j - 3], ' and ', ty^^[j - 1], ' are not both operand tokens');
goto 993;
end;
setnodefields(j - 2, j - 3, j - 1);
setnodetoken(j - 3);
j := j - 3;
reset(j + 1, jtot, 2);
goto 992;
end;
if ty^^[j] = rightparen then
begin
if (ty^^[j - 2] <> leftparen) or (not s2) then
begin
error := ' ty^^[j-2] <> leftparen token or ty^^[j-1] <> an operand token';
error := concat(ty^^[j - 2], ' is not a left parenthesis token or ', ty^^[j - 1], ' is not an operand token');
goto 993;
end;
ty^^[j - 2] := ty^^[j - 1];
typr^^[j - 2] := typr^^[j - 1];
typ^^[j - 2] := typ^^[j - 1];
j := j - 2;
reset(j + 1, jtot, 2);
j := j - 2;
end;
992:
end;
until ty^^[j] = semicolon;
if j <> 2 then
error := 'possible incorrect pairing of parentheses';